package likong.bibliothek.pages.autor;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.Model;

import likong.bibliothek.obj.Autor;
import likong.bibliothek.pages.SelectModalWindow;

import db.HibernateHelper;

public class AutorWeb extends AutorAbstract {

	public AutorWeb(){
		super();
		
		// Alle Autoren abfragen
		List<Autor> result = HibernateHelper.listAll(Autor.class);

		PropertyListView<Autor> view = new PropertyListView<Autor>(
				"authorShow", result) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Autor> item) {
				PageParameters params = new PageParameters();
				params.add("autorID", Integer.toString(item.getModel().getObject().getAuthorID()));
				BookmarkablePageLink<WebPage> loeschen = new BookmarkablePageLink<WebPage>("loeschen", AutorLoeschen.class, params);
				item.add(loeschen);
				BookmarkablePageLink<WebPage> link =new BookmarkablePageLink<WebPage>("bearbeiten", AutorEdit.class, params);
				item.add(link);
				item.add(new Label("name"));
				item.add(new Label("vorname"));
				loeschen.add(new Image("image",new ResourceReference(this.getClass(),"Remove.ico")));
			}

		};
		add(view);
		//Versuch ein Popupfenster zu erstellen.
		//TODO: Weiter ausarbeiten.
		// The label that shows the result from the ModalWindow
        final Label resultLabel = new Label("resultlabel", new Model(""));
        resultLabel.setOutputMarkupId(true);
        add(resultLabel);



        // The ModalWindow, showing some choices for the user to select.
        final ModalWindow selectModalWindow = new SelectModalWindow("modalwindow"){

            public void onSelect(AjaxRequestTarget target, String selection) {
                // Handle Select action
                resultLabel.setDefaultModelObject(selection);
                target.addComponent(resultLabel);
                close(target);
            }

            public void onCancel(AjaxRequestTarget target) {
                // Handle Cancel action
                resultLabel.setDefaultModelObject("ModalWindow cancelled.");
                target.addComponent(resultLabel);
                close(target);
            }

        };
        add(selectModalWindow);



        // The AjaxLink to open the modal window
        add(new AjaxLink("linkToModalWindow"){
            public void onClick(AjaxRequestTarget target) {
                selectModalWindow.show(target);
            }
        });
		
		// Blendet das übergeordnete Enclosure (Tabelle, überschrift) aus, wenn
		// Ausgabe leer!
		view.setVisible(result.size() > 0 ? true : false);
	}
}

	
